package com.jh.browser.libs;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.jh.browser.application.MyApplication;
import com.jh.browser.common.Common;
import com.jh.browser.common.Constant;

public class AndroidBug5497Workaround {
    /**
     * 定义键盘状态更改监听器（接口）
     */
    public interface OnKeyBoardChangeListener {
        /**
         * 键盘状态更改方法
         *
         * @param show 键盘是否显示
         */
        void OnKeyBoardChange(boolean show);
    }

    /**
     * 键盘状态监听事件对象
     */
    private static OnKeyBoardChangeListener onKeyBoardChangeListener;
    /**
     * 底部有navigation的导航高度
     */
    private static int height = 0;
    /**
     * 首次高度差值
     */
    private static int firstHeightDifference = 0;

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    public static void assistActivity(Activity activity, OnKeyBoardChangeListener onKeyBoardChangeListener) {
        new AndroidBug5497Workaround(activity);
        AndroidBug5497Workaround.onKeyBoardChangeListener = onKeyBoardChangeListener;
        initHeight();
    }

    /**
     * 初始化默认高度
     */
    public static void initHeight() {
        int height = 0;
        if (!Common.getPreferences(MyApplication.getContext(), Constant.SET_FULLSCREEN, false)) {
            //全屏
            height = Common.getStatusHeight();
        }
        AndroidBug5497Workaround.height = height;
    }

    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (firstHeightDifference == 0) {
                firstHeightDifference = heightDifference;
            }
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // 键盘显示
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                onKeyBoardChangeListener.OnKeyBoardChange(true);
            } else {
                // 键盘隐藏
                if (Common.getPreferences(MyApplication.getContext(), Constant.SET_FULLSCREEN, false)) {
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference - height;
                } else {
                    if ((firstHeightDifference + height) == heightDifference) {
                        //模拟器
                        frameLayoutParams.height = usableHeightSansKeyboard - firstHeightDifference - height;
                    } else {
                        frameLayoutParams.height = usableHeightSansKeyboard - height;
                    }
                }
                onKeyBoardChangeListener.OnKeyBoardChange(false);
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }
}
